package cn.wolfcode.wolf2w.redis.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.exception.LogicalException;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.KeyManager;
import cn.wolfcode.wolf2w.vo.StrategyStatisVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
//@Transactional
public class StrategyRedisServiceImpl implements IStrategyRedisService {

    @Autowired
    private StringRedisTemplate template;
    
    @Autowired
    private IStrategyService strategyService;


    @Override
    public void viewnemIncrease(String id, int num) {
        //获取vo
        StrategyStatisVO vo = this.getStatisVo(id);
        //将对象里的阅读数添加给vo
        vo.setViewnum(vo.getViewnum() + num);
        //缓存vo 到 redis
        this.setStatisVo(vo);

    }

    @Override
    public StrategyStatisVO getStatisVo(String sid) {
        //校验参数
        if (!StringUtils.hasLength(sid)){
            throw new LogicalException("参数异常");
        }

        //获取key
        String key = KeyManager.STRATEGY_STATIS_VO.join(sid);

        //新建对象
        StrategyStatisVO vo = null;

        //判断是否有key
        if (template.hasKey(key)){
            //有
            //根据key 从redis 获取 value,
            String strVO = template.opsForValue().get(key);
            vo = JSON.parseObject(strVO, StrategyStatisVO.class);
        }else {
            //如果不存在
            vo = new StrategyStatisVO();
            //获取攻略对象
            Strategy strategy = strategyService.get(sid);
            //将strategy赋值给vo
            BeanUtils.copyProperties(strategy, vo);
            //设置vo的strategyId
            vo.setStrategyId(strategy.getId());

        }

        return vo;
    }

    @Override
    public void setStatisVo(StrategyStatisVO vo) {
        //获取key
        String key = KeyManager.STRATEGY_STATIS_VO.join(vo.getStrategyId());

        //设置到缓存中
        template.opsForValue().set(key, JSON.toJSONString(vo));

    }


    @Override
    public void replynumIncrease(String sid, int num) {
        //获取vo对象
        StrategyStatisVO vo = this.getStatisVo(sid);
        //评论数加1
        vo.setReplynum(vo.getReplynum() + num);
        //设值给vo
        this.setStatisVo(vo);

    }

    @Override
    public boolean StrategyFavor(String sid, String uid) {
        if (!StringUtils.hasLength(sid)){
            throw new LogicalException("参数异常");

        }
       //2.枚举类新建字段,以uid拼接key
        String key = KeyManager.STRATEGY_STATIS_FAVOR.join(uid);
        //4.不存在,新建一个sid集合
        List<String> sidList = new ArrayList<>();
       //3.判断是否存在key
        if (template.hasKey(key)){
            //5.存在,通过key获取sid集合
            String sidsStr = template.opsForValue().get(key);
            //转换为list
            sidList = JSON.parseArray(sidsStr,String.class);
        }

        //获取vo对象
        StrategyStatisVO vo = this.getStatisVo(sid);
        //6.判断sid是否在这个集合中
        if (sidList.contains(sid)){
            //7.存在,为取消收藏操作,收藏数-1,将sid移除集合
            vo.setFavornum(vo.getFavornum()-1);
            sidList.remove(sid);
        }else {
            //8.不存在,为收藏操作, 收藏数+1, 将sid存到集合
            vo.setFavornum(vo.getFavornum()+ 1);
            sidList.add(sid);
        }
       //9,将key和sid 集合缓存到redis
        template.opsForValue().set(key, JSON.toJSONString(sidList));
       //10.将vo更新
        this.setStatisVo(vo);
        //包含为添加收藏, 不包含为取消收藏
        return sidList.contains(sid);
    }

    @Override
    public List<String> getStatisFavor(String uid) {

        //2.枚举类新建字段,以uid拼接key
        String key = KeyManager.STRATEGY_STATIS_FAVOR.join(uid);
        //4.不存在,新建一个sid集合
        List<String> sidList = new ArrayList<>();
        //3.判断是否存在key
        if (template.hasKey(key)){
            //5.存在,通过key获取sid集合
            String sidsStr = template.opsForValue().get(key);
            //转换为list
            sidList = JSON.parseArray(sidsStr,String.class);
        }

        return sidList;
    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {
       //2.在枚举类中添加字段, 用uid和sid拼接key
        String key = KeyManager.STRATEGY_STATIS_THUMBUP.join(uid,sid);

        //最后一秒
        Date end = DateUtil.getEndDate(new Date());
        //现在时间
        Date now = new Date();
        Long time = DateUtil.getDateBetween(end, now);

       //3.判断是否存在key

        if (!template.hasKey(key)){
            //5.不存在,获取vo对象,点赞数+1,将key和任意value存到redis中, 并设置有效时间
            StrategyStatisVO vo = this.getStatisVo(sid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            template.opsForValue().set(key, "1", time, TimeUnit.SECONDS);
            //更新vo
            this.setStatisVo(vo);
            return true;
        }
       //4.存在,不做任何操作


        return false;
    }

    @Override
    public List<StrategyStatisVO> queryStatisVOS() {

        //1.获取所有的key
        String pattern = KeyManager.STRATEGY_STATIS_VO.join("*");
        Set<String> keys = template.keys(pattern);

        //新建一个集合
        List<StrategyStatisVO> list = new ArrayList<>();
        if (keys != null && keys.size() >0){
            //2.遍历key,获取每个vo对象
            for (String key : keys) {
                String voStr = template.opsForValue().get(key);
                StrategyStatisVO vo = JSON.parseObject(voStr, StrategyStatisVO.class);
                //vo放到集合
                list.add(vo);

            }
        }

        return list;
    }


}
